// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-06 10:12:16
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 15:02:35
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Constraint/TEMPParser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/TEMPParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/ITEMPService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (TEMP);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/TEMP/TEMP.md>
*/

SG::DataStructure::Common::Status BDF::BULKDATA::ParseTEMP (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    // 定义两个 TEMPData 类型的临时对象，用于存储解析出的单点约束数据
    SG::DataStructure::FEM::TEMPData TEMPata;

    // TODO(bug 001) m_dominId的正确填写
    TEMPata.m_domainId = 1;

    TEMPata.m_id = convertToI64 (dividedEntryInfo[1]);

    for (size_t i = 2; i < 8; i += 2)
    {
        bool const isEmpty = is_all_space (dividedEntryInfo[i]);
        if (isEmpty)
        {
            continue;
        }
        TEMPata.m_G = convertToI64 (dividedEntryInfo[i]);
        TEMPata.m_T = convertToReal (dividedEntryInfo[i + 1]);
        if (dbServiceFactory->get<ITEMPService> ()->Add (TEMPata) == Common::Status::STATUS_Failure)
        {
            throw std::runtime_error ("Failed to add TEMP data to database.");
            return Common::Status::STATUS_Failure;
        }
    }

    // 将第一个节点的约束数据添加到数据库
    return Common::Status::STATUS_Success;
}